<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="crud-group">
      <!-- 搜索 -->
      <el-select
        v-model="query.fileAffiliation"
        clearable
        size="small"
        placeholder="请选择归属"
        class="filter-item"
      >
        <el-option
          v-for="item in fileAffiliationList"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-input
        v-model="query.scanFileName"
        clearable
        size="small"
        placeholder="文件名称"
        style="width:200px"
        class="filter-item"
      />
      <rrOperation :crud="crud" />
      <!-- @click="exportExcel1(crud.selections)" -->
      <el-button
        class="filter-item"
        size="mini"
        type="primary"
        :loading="this.downLoading === true"

        @click="exportExcel1(crud.data)"
      >下载结果</el-button>
      <el-tag
        style="margin-left: 0.6rem;"
        size="large"
        type="warning"
      >* 扫描过程中该页面可以关闭</el-tag>
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      max-height="350px"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column
        type="selection"
        width="45px"
      />
      <el-table-column
        label="文件包名"
        prop="scanPackName"
        width="145px"
      />
      <el-table-column
        label="文件名称"
        prop="scanFileName"
      />
      <el-table-column
        label="扫描状态"
        prop="scanState"
        width="110px"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.scanState==='SUCCESS'"
            size="small"
            type="success"
          >成功</el-tag>
          <el-tag
            v-if="scope.row.scanState==='SCANNING'"
            size="small"
          >扫描中</el-tag>
          <el-tag
            v-if="scope.row.scanState==='UNSCAN'"
            size="small"
            type="info"
          >待扫描</el-tag>
          <el-tag
            v-if="scope.row.scanState==='FAIL'"
            size="small"
            type="danger"
          >异常</el-tag>
          <el-tag
            v-if="scope.row.scanState==='WARNING'"
            size="small"
            type="warning"
          >告警</el-tag>
          <el-tag
            v-if="scope.row.scanState==='IGNORE'"
            size="success"
          >告警已忽略</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="扫描开始时间"
        prop="createTime"
        width="140px"
      />
      <el-table-column
        label="扫描结束时间"
        prop="lastUpdateTime"
        width="140px"
      />
      <el-table-column
        label="扫描明细"
        width="160px"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="lookScanList(scope.row)"
          >查看扫描明细</el-button>
          <el-button
            type="text"
            :disabled="scope.row.scanState!=='WARNING'"
            @click="ignoreWarning(scope.row)"
          >一键忽略</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex; align-items: center; justify-content: end;">
      <!--分页组件-->
      <pagination />
    </div>

    <div
      style="display: flex; align-items: center; justify-content: center;"
    >
      <el-button @click="$emit('pre')">上一步</el-button>
      <el-button
        :disabled="!canCreatePublishPack"
        type="primary"
        @click="$emit('next')"
      >完成</el-button>
    </div>

    <scan-detail
      v-if="lookSacnDetailFlag"
      :scan-list-id="scanListId"
      :dialog-visible="lookSacnDetailFlag"
      :scan-file-name="scanFileName"
      @close="()=>{
        cancelCU()
        lookSacnDetailFlag = false
      }"
    />

  </div>
</template>

<script>
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/extend/script/pack/RR.operation-scanResult'
import pagination from '@crud/Pagination'
import scriptPackScanListApi from '@/api/script/pack/scriptPackScanList'
import scriptPackScanResultApi from '@/api/script/pack/scriptPackScanResult'
import scanDetail from '../scanDetail/index'
import { downloadFile } from '@/utils/index'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '脚本扫描确认清单', url: 'api/scriptPack/list/page', crudMethod: { ...scriptPackScanListApi }})
const defaultForm = {}
export default {
  name: 'ScriptCheckResult',
  components: { rrOperation, pagination, scanDetail },
  props: {
    packTaskId: {
      type: String,
      default: null
    }
  },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      canCreatePublishPack: false,
      scanPackName: '',
      checkResultRef: null,
      downLoading: false,
      scanListId: null,
      scanFileName: '',
      lookSacnDetailFlag: false,
      fileAffiliationList: [
        { key: 'init', value: '初始化脚本' },
        { key: 'period', value: '周期脚本' },
        { key: 'service', value: '下载、推送、稽核脚本' }
      ]
    }
  },
  created() {
  },
  methods: {
    [CRUD.HOOK.afterRefresh](crud) {
      this.canCreatePublishPack = crud.data.length > 0
    },
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.scriptPackTaskId = this.packTaskId
    },
    lookScanList(row) {
      this.lookSacnDetailFlag = true
      this.scanListId = row.id
      this.scanFileName = row.scanFileName
    },
    resetQueryData() {
      this.crud.resetQuery()
    },
    cancelCU() {
      this.lookSacnDetailFlag = false
      this.crud.refresh()
    },
    // 一键忽略异常
    ignoreWarning(row) {
      scriptPackScanListApi.ignoreById(row.id).then(res => {
        this.$notify({ title: '成功', message: '一键忽略告警级别异常成功！', type: 'success' })
        this.crud.refresh()
      })
    },
    exportExcel(row) {
      if (this.checkScanStatus(row)) {
        this.exportResult(row)
      } else {
        this.$notify({ title: '警告', message: '存在尚未完成的任务', type: 'warning' })
      }
    },
    exportExcel1(row) {
      if (row.length > 0 && row.some(item => item.scanState === 'SCANNING' || item.scanState === 'UNSCAN')) {
        this.$notify({ title: '警告', message: '存在尚未扫描完成的任务，请稍后下载', type: 'warning' })
      } else {
        this.exportResult(row)
      }
    },
    // 导出结果清单
    exportResult(row) {
      this.downLoading = true
      const taskId = this.packTaskId
      const listId = []
      if (row.length !== 0) {
        row.forEach(listInfo => {
          listId.push(listInfo.id)
        })
      } else {
        listId.push(null)
      }
      const params = {
        listId: listId,
        taskId: taskId
      }
      crud.downloadLoading = true
      this.$notify({ title: '导出中', message: '正在导出扫描结果清单', type: 'success' })
      scriptPackScanResultApi.download(params).then(result => {
        downloadFile(result, '脚本扫描结果', 'zip')
        this.downLoading = false
        crud.downloadLoading = false
      }).catch(() => {
        // this.$notify({ title: '警告', message: '存在尚未扫描完成的任务，请稍后下载', type: 'warning' })
        this.downLoading = false
        crud.downloadLoading = false
      })
    },
    async checkScanStatus(selections) {
      if (selections.length > 0) {
        return selections.every(item => item.scanState !== 'SCANNING' && item.scanState !== 'UNSCAN')
      } else {
        const allList = await scriptPackScanListApi.getListByTaskId(this.packTaskId)
        return allList.every(item => item.scanState !== 'SCANNING' && item.scanState !== 'UNSCAN')
      }
    }
  }
}
</script>
<style scoped>

</style>
